// pages/personalmsg/personalmsg.js
import {
    getuserinfoapi,
    getusermanageprojectapi
} from '../api/personalMsg.js'
import user from '../../utils/user'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasnoselect: false,
        id: '',
        username: '',
        userimg: '',
        userphone: '',
        hasnextpage: true,
        companyList: [],
        pageNum: 1,
        pageSize: 10,
        isup: false,
        innerlist: [],
        innerpagesize: 10,
        innerpagenum: 1,
        innerhasnext: true
    },
    async getuserinfo() {
        let res = await getuserinfoapi({
            openId: user.openid,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
        })
        console.log(res)
        console.log(res.companyList)
        let arr = []
        res.companyList.list.forEach((ele, i) => {
            if (!ele.isSelected) {
                arr.push(ele)
            }
        })
        if (arr.length === 0) {
            this.setData({
                hasnoselect: true
            })
        }
        this.setData({
            companyList: this.data.companyList.concat(res.companyList.list),
            username: res.userName,
            userimg: res.userHeadUrl,
            userphone: res.phone,
            hasnextpage: res.companyList.hasNextPage
        })
    },
    naexpage() {
        console.log('下一页')
        if (this.data.innerhasnext) {
            this.data.innerpagenum += 1
            this.getusermanageproject()
        }
    },
    opupopup(e) {
        console.log(e)
        this.data.innerlist = []
        this.data.innerpagenum = 1
        this.data.innerhasnext = true
        this.data.id = e.currentTarget.dataset.id
        if (e.currentTarget.dataset.count * 1 === 0) {
            wx.showToast({
                title: '暂无项目',
                icon: 'none'
            })
            return
        } else {
            this.getusermanageproject()
            this.setData({
                isup: true
            })
        }
    },
    async getusermanageproject() {
        let res = await getusermanageprojectapi({
            companyId: this.data.id,
            pageSize: this.data.innerpagesize,
            pageNum: this.data.innerpagenum,
        })
        console.log(res)
        this.setData({
            innerlist: this.data.innerlist.concat(res.list),
            innerhasnext: res.hasNextPage
        })
    },
    toinfo(e) {
        console.log(e.currentTarget.dataset)
        let obj = e.currentTarget.dataset
        wx.navigateTo({
            url: `/pages/companymanagement/companymanagement?isadmin=${obj.isadmin}&name=${obj.name}&area=${obj.area}&supadmin=${obj.supadmin}&status=${obj.status}&isselect=${obj.isselect}&companyid=${obj.companyid}`,
        })
    },
    closepopup() {
        console.log(11)
        this.setData({
            isup: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            hasnextpage: true,
            pageNum: 1,
            companyList: []
        })
        this.getuserinfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.hasnextpage) {
            this.data.pageNum += 1
            this.getuserinfo()
            console.log(this.data.pageNum)
        } else {
            wx.showToast({
                title: '没有更多数据了',
                icon: 'none'
            })
            return
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})